<template>
  <main-box one="商品管理" two="商品列表">
    <!--栅格布局-->
    <el-row :gutter="20">
      <el-col :span="9">
        <el-input placeholder="请输入内容" v-model="goodsForm.query" clearable>
          <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
        </el-input>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" @click="$router.push('./add')">添加商品</el-button>
      </el-col>
    </el-row>

    <!-- table表格 -->
    <el-table :data="goodsList" style="width: 100%" border stripe>
      <el-table-column type="index"></el-table-column>
      <el-table-column prop="goods_name" label="商品名称"></el-table-column>
      <el-table-column prop="goods_price" label="商品价格(元)" width="100px"></el-table-column>
      <el-table-column prop="goods_weight" label="商品重量" width="100px"></el-table-column>
      <el-table-column prop="add_time" label="创建时间" width="100px">
        <template slot-scope="scope">{{scope.row.add_time | dateFormat }}</template>
      </el-table-column>
      <el-table-column label="操作" width="130px">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="deleteGoodsById(scope.row.goods_id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="goodsForm.pagenum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="goodsForm.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </main-box>
</template>



<script>
import moment from "moment";
export default {
  data() {
    return {
      goodsForm: {
        query: "",
        pagenum: 1,
        pagesize: 10
      },
      goodsList: [],
      total: 0
    };
  },
  mounted() {
    this.getGoodsList();
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.goodsForm
      });
      // console.log(res);
      if (res.meta.status != 200) {
        return this.$message.error("获取数据失败");
      }
      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },
    handleSizeChange(newSize) {
      this.goodsForm.pagesize = newSize;
      this.getGoodsList();
    },
    handleCurrentChange(newNum) {
      this.goodsForm.pagenum = newNum;
      this.getGoodsList();
    },
    async deleteGoodsById(id) {
      console.log(id);
      const confirmResult = await this.$confirm(
        "此操作将永久删除该商品, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      // console.log(confirmResult);
      if (confirmResult == "comfirm")
      {
        this.$message.info("已取消删除");
      } else{
           const {data:res} = await this.$http.delete(`goods/${id}`);
        console.log(res);
        if (res.meta.status != 200) {
          return this.$message.error("删除失败");
        }
        this.$message.success("删除成功");
        this.getGoodsList();
      }
       
      
    }
  },
  filters: {
    dateFormat(str) {
      return moment(str).format("YYYY-MM-DD HH:mm:ss");
    }
  }
};
</script>
<style lang="less" scope>
</style>